//
//  HJStatusesCell.swift
//  sinaWeibo
//
//  Created by hjun on 16/3/26.
//  Copyright © 2016年 hjun. All rights reserved.
//

import UIKit
import SnapKit
class HJStatusesCell: UITableViewCell {
    
    //MARK: 属性
    //记录底部约束
    var retweetViewCon: Constraint?
    
    
    ///微博模型
    var status: HJStatus?{
        
        didSet{
            ///赋值给微博模型
            originalView.status = status
            
            ///先将之前的约束删掉
            retweetViewCon?.uninstall()
            
            if let retweetStatus = status?.retweeted_status{
                
                retweetView.hidden = false
                ///赋值
                retweetView.retweetStatus = retweetStatus
                
                ///重新约束
                
                bottomView.snp_makeConstraints { (make) -> Void in
                    self.retweetViewCon = make.top.equalTo(retweetView.snp_bottom).constraint
                }
                
            }else {
                
                retweetView.hidden = true
                
                
                bottomView.snp_makeConstraints { (make) -> Void in
                    self.retweetViewCon = make.top.equalTo(originalView.snp_bottom).constraint
                    
                }
            }
            
        }
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
        
        
        
    }
    private func prepareUI(){
        
        ///添加子控件
        
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        ///添加约束
        
        originalView.snp_makeConstraints { (make) -> Void in
            
            make.top.trailing.leading.equalTo(contentView)
            
            
        }
        
        
        retweetView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(originalView.snp_bottom).offset(HJPictureViewMargin)
            make.leading.trailing.equalTo(contentView)
            
        }
        
        
        bottomView.snp_makeConstraints { (make) -> Void in
            self.retweetViewCon = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(44)
        }
        
        
        ///给contentView添加约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.trailing.leading.equalTo(self)
            
            make.bottom.equalTo(bottomView)
            
        }
        
        
    }
    
    
    //MARK: 懒加载
    ///原创微博view
    private  lazy var originalView: HJOriginalView = HJOriginalView()
    private lazy var bottomView:HJStatusBottomView = HJStatusBottomView()
    
    private lazy var retweetView: HJRetweetedView = HJRetweetedView()
}
